<?php
/**
 * Created by JetBrains NetBeans.
 * User: yinyancai
 * Date: 13-3-14 下午4:44
 * To change this template use File | Settings | File Templates.
 */
if (!defined('IN_MOBILE_API')) {
    exit('Access Denied');
}
include_once 'forum.php';
class cloud_api {
    function common() { }

    function output() {

        global $_G;

        if(empty($_GET['cadid'])){
            $variable = array(
                'code' => 3008,
                'messagestr' => lang('plugin/cloud', '3008'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }else{
            $area = C::t('#cloud#cloud_app_district')->fetch_info($_GET['cadid']);
            $app = C::t('#cloud#cloud_app')->fetch_app_info($area['caid']);
            if($app['disabled']==0){
                $variable = array(
                    'code' => 14006,
                    'messagestr' => lang('plugin/cloud', '14006'),
                );
                cloud_core::result(cloud_core::variable($variable));
            }

        }

        //判断是否缺少参数
        if (empty($_GET['cuid']) || empty($_GET['cadid']) || empty($_GET['orderer']) ||
            empty($_GET['otel']) || empty($_GET['consignee']) || empty($_GET['ctel'])
            || empty($_GET['takedate']) || empty($_GET['taketime']) || empty($_GET['takeaddress']) ||empty($_GET['cartids'])
        ) {
            $variable = array(
                'code' => 1001,
                'messagestr' => lang('plugin/cloud', '1001'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        $takedate = explode('T', $_GET['takedate']);
        $taketime = explode('T', $_GET['taketime']);

        //新增
        $data = array(
            'cuid' => $_GET['cuid'],
            'cadid' => $_GET['cadid'],
            'createtime' => TIMESTAMP,
            'totalmoney' => $_GET['totalmoney'],
            'paymenttype' => $_GET['paymenttype'],
            'paymentstatus' => '未支付',
            'picktype' => $_GET['picktype'],
            'status' => '未处理',
            'lasttime' => TIMESTAMP,
            'orderer' => $_GET['orderer'],
            'otel' => $_GET['otel'],
            'consignee' => $_GET['consignee'],
            'ctel' => $_GET['ctel'],
            'takedate' => $takedate[0],
            'taketime' => $taketime[1],
            'takeaddress' => $_GET['takeaddress'],
            'remark' => $_GET['remark'],
            'messages' => $_GET['messages'],
            'isbill' => $_GET['isbill'],

        );
        //生成订单
        $orders_id = C::t('#cloud#cloud_orders')->insert($data);
        //如果插入失败，这返回错误信息
        if (empty($orders_id)) {
            $variable = array(
                'code' => 14000,
                'messagestr' => lang('plugin/cloud', '14000'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        else {
            //添加订单产品
            $cartid_array = explode(',', $_GET['cartids']);
            foreach ($cartid_array as $k => $v) {
                //查询购物车上订单产品数据
                $cart = C::t('#cloud#cloud_shopping_cart')->fetch_info($v);
                //添加订单产品表
                $date = array(
                    'cpid' => $cart['cpid'],
                    'pname' => $cart['pname'],
                    'picurl' => $cart['picurl'],
                    'cpsid' => $cart['cpsid'],
                    'sname' => $cart['sname'],
                    'price' => $cart['price'],
                    'countprice' => $cart['countprice'],
                    'pnumber' => $cart['pnumber'],
                    'coid' => $orders_id,
                );
                $res = C::t('#cloud#cloud_orders_item')->insert($date);
                //清空购物车数据
                if ($res)
                    C::t('#cloud#cloud_shopping_cart')->delete_by_id($v);
            }

            //生成订单编号
            $ordersNo=date('Y') . date('m') . date('d') . date('H') . date('i') . $orders_id;
            $updateDate = array(
                'ordersNo' => $ordersNo,
            );
            C::t('#cloud#cloud_orders')->update($orders_id, $updateDate);

            if (empty($_GET['selectConsignee'])) {
                $shippingAddress = array(
                    'name' => $_GET['consignee'],
                    'address' => $_GET['takeaddress'],
                    'mobile' => $_GET['ctel'],
                    'cuid' => $_GET['cuid'],
                    'cadid' => $_GET['cadid'],
                    'lasttime' => TIMESTAMP,
                );
                C::t('#cloud#cloud_shipping_address')->insert($shippingAddress);
            }
        }

        //正常返回
        $variable = array(
            'data'=>$ordersNo,
            'messagestr' => lang('plugin/cloud', '14001'),
        );
        cloud_core::result(cloud_core::variable($variable));
    }
}

?>

